package edu.nuc.wordcount2;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * Time    : 2023/4/2 8:24
 * Author  : 王摇摆
 * FileName: MPJob.java
 * Software: IntelliJ IDEA 2020.2.2
 * Blog    ：https://blog.csdn.net/weixin_44943389?type=blog
 */

public class MPJob {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration configuration = new Configuration();
        configuration.set("fs.defaultFS", "file:///");
        configuration.set("mapreduce.framework.name", "local");
        System.setProperty("HADOOP_USER_NAME", "local");

        Job job = null;
        try {
            job = Job.getInstance(configuration, "wordCount2");
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (job != null) {
            job.setJarByClass(MPJob.class);
        }
        job.setMapperClass(MyMapper.class);
        job.setReducerClass(MyReducer.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        FileInputFormat.setInputPaths(job, new Path("C:\\Users\\Administrator\\Desktop\\Code\\learn_hadoop\\input.txt"));
        FileOutputFormat.setOutputPath(job, new Path("E:\\output\\output04"));

        job.setNumReduceTasks(3);
        boolean b = job.waitForCompletion(true);

        if (b)
            System.out.println("MapReduce正常运行结束。");
        else
            System.out.println("MapReduce异常运行结束。");
    }
}
